/* main.c */
#include "app_inc.h"

#define APP_SDCARD_START_SECTOR_ID   4u

SDC_Info_T gAppSDCardInfoStruct;
uint8_t    gAppSDCardTxBuf[SDC_BLOCK_SIZE_BYTE * 2u];
uint8_t    gAppSDCardRxBuf[SDC_BLOCK_SIZE_BYTE * 2u];

static void App_SDCard_Init(void);
static void App_SDCard_Probe(void);
static void App_SDCard_ReadSingleBlock(void);
static void App_SDCard_WriteSingleBlock(void);
static void App_SDCard_ReadMultileBlocks(void);
static void App_SDCard_WriteMultipleBlocks(void);

int main(void)
{
    
    BSP_InitSystem();
    BSP_InitStdioLPUART(115200UL);
    
    printf("\r\n\r\nFatFS Example\r\n");
    printf("Compiled at %s on %s\r\n", __TIME__, __DATE__);
    
    /* 初始化SD卡 */
    App_SDCard_Init();
    
    /* 识别SD卡 */
    App_SDCard_Probe();
    
    /* 单块读数据 */
    App_SDCard_ReadSingleBlock();

    /* 单块写数据并验证 */
    App_SDCard_WriteSingleBlock();
    App_SDCard_ReadSingleBlock();

    /* 多块读 */
    App_SDCard_ReadMultileBlocks();
    
    /* 多块写数据并验证 */
    App_SDCard_WriteMultipleBlocks();
    App_SDCard_ReadMultileBlocks();
    
    printf("ALL TEST DONE.\r\n");
    
    while (1)
    {
        
    }
}

static void App_SDCard_Init(void)
{
    BSP_InitLPSPI_SDCard(); /* 初始化引脚和时钟 */
    SDC_Install(&gBspSdcSpiHwDrvStruct);
    
}

static void App_SDCard_Probe(void)
{
    printf("SDC_InitCard() : ");
    if (!SDC_InitCard(&gAppSDCardInfoStruct))
    {
        printf("Failed.\r\n");
        while (1);
    }
    else
    {
        printf("CardType(%d), CardBlkCnt(%d), %dMB\r\n",
            gAppSDCardInfoStruct.CardType,
            gAppSDCardInfoStruct.CardBlkCnt,
            gAppSDCardInfoStruct.CardBlkCnt / 2u / 1024
        );
    }
}

static void App_SDCard_ReadSingleBlock(void)
{
    uint32_t i;

    printf("SDC_ReadBlock(%d) : ", APP_SDCARD_START_SECTOR_ID);
    if ( !SDC_ReadBlock(APP_SDCARD_START_SECTOR_ID, gAppSDCardRxBuf) )
    {
        printf("Failed.\r\n");
        while (1);
    }
    else
    {
        printf("OK.\r\n");
        
        for (i = 0u; i < SDC_BLOCK_SIZE_BYTE; i++)
        {
            printf("0x%2X ", gAppSDCardRxBuf[i]);
        }
        printf("\r\n");
    }
}

static void App_SDCard_WriteSingleBlock(void)
{
    uint32_t i;

    printf("SDC_WriteBlock(%d) : ", APP_SDCARD_START_SECTOR_ID);
    for (i = 0u; i < SDC_BLOCK_SIZE_BYTE; i++)
    {
        gAppSDCardTxBuf[i] = (gAppSDCardRxBuf[i]+1u) % 256u;
    }
    if ( !SDC_WriteBlock(APP_SDCARD_START_SECTOR_ID, gAppSDCardTxBuf) )
    {
        printf("Failed.\r\n");
        while (1);
    }
    else
    {
        printf("OK\r\n");
    }
}

static void App_SDCard_ReadMultileBlocks(void)
{   
    uint32_t i;
    printf("SDC_ReadBlocks(%d, %d) : ", APP_SDCARD_START_SECTOR_ID, APP_SDCARD_START_SECTOR_ID+1u);
    if ( 0u != SDC_ReadBlocks(APP_SDCARD_START_SECTOR_ID, 2u, gAppSDCardRxBuf) )
    {
        printf("Failed.\r\n");
        while (1);
    }
    else
    {
        printf("OK.\r\n");
        
        for (i = 0u; i < SDC_BLOCK_SIZE_BYTE*2u; i++)
        {
            printf("0x%2X ", gAppSDCardRxBuf[i]);
        }
        printf("\r\n");
    }
}

static void App_SDCard_WriteMultipleBlocks(void)
{
    uint32_t i;
    
    printf("SDC_WriteBlocks(%d, %d) : ", APP_SDCARD_START_SECTOR_ID, APP_SDCARD_START_SECTOR_ID+1u);
    for (i = 0u; i < SDC_BLOCK_SIZE_BYTE*2u; i++)
    {
        gAppSDCardTxBuf[i] = (gAppSDCardRxBuf[i]+1u) % 256u;
    }
    if ( 0u != SDC_WriteBlocks(APP_SDCARD_START_SECTOR_ID, 2u, gAppSDCardTxBuf) )
    {
        printf("Failed.\r\n");
        while (1);
    }
    else
    {
        printf("OK.\r\n");
    }
}

/* EOF. */
